package com.xinsoft.controller.device;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.CusUpkeepParam;
import com.xinsoft.entity.po.CusUpkeep;
import com.xinsoft.entity.po.CusUpkeepAudit;
import com.xinsoft.entity.vo.CusUpkeepVo;
import com.xinsoft.service.CusUpkeepAuditService;
import com.xinsoft.service.CusUpkeepService;
import com.xinsoft.utils.TokenUtil;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @ClassName CusUpKeepOrderController
 * @Description 保养执行控制层
 * @Author lijie
 * @Date 2023-6-19 12:52:31
 * @Version 1.0
 */
@RestController
@RequestMapping("/cus/upKeep/order")
@Api(value = "CusUpKeepOrderController", tags = {"保养执行控制层"})
public class CusUpKeepOrderController {
    @Autowired
    private CusUpkeepService upkeepService;

    @Autowired
    private CusUpkeepAuditService auditService;

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养执行-暂存
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("暂存")
    @PostMapping("/add")
    public Response<?> add(@RequestBody CusUpkeep cusUpkeep) {
        // 参数校验
        ValidatorUtils.validateEntity(cusUpkeep);
        upkeepService.tempSave(cusUpkeep);
        return Response.succeed("操作成功");
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养执行-提交
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("提交")
    @PostMapping("/submit")
    public Response<?> submit(@RequestBody CusUpkeep cusUpkeep) {
        // 参数校验
        ValidatorUtils.validateEntity(cusUpkeep);
        upkeepService.submit(cusUpkeep);
        return Response.succeed("操作成功");
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养执行-执行
     * @Date 2023/5/17 9:57
     * @params dept
     **/
    @ApiOperation("完成")
    @PostMapping("/finish")
    public Response<?> finish(@RequestBody CusUpkeep cusUpkeep) {
        // 参数校验
        ValidatorUtils.validateEntity(cusUpkeep);
        upkeepService.finish(cusUpkeep);
        return Response.succeed("操作成功");
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 关闭保养任务
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @GetMapping("/close")
    public Response<?> close(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        CusUpkeep cusUpkeep = upkeepService.getById(id);
        if (cusUpkeep == null) {
            return Response.fail("执行任务不存在");
        }
        cusUpkeep.setUpkeepStatus(Const.UPKEEP_STATUS_CLOSE);
        upkeepService.updateById(cusUpkeep);
        return Response.succeed();
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 获取详情
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("获取详情")
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        CusUpkeepVo cusUpkeep = upkeepService.getByInfoById(id);
        return Response.succeed(cusUpkeep);
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 扫码获取设备当前是否有保养任务
     * @Date 2023/5/17 9:57
     * @params id
     **/
    @ApiOperation("扫码获取设备当前是否有保养任务")
    @GetMapping("/getTaskByDeviceId")
    public Response<?> getTaskByDeviceId(@RequestParam(value = "deviceId", defaultValue = "", required = true) Integer deviceId) {
        CusUpkeepVo cusUpkeep = upkeepService.getTaskByDeviceId(deviceId);
        return Response.succeed(cusUpkeep);
    }

    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 删除
     * @Date 2023/5/12 9:20
     * @params ids
     **/
    @ApiOperation("删除")
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        upkeepService.removeByIds(ids);
        return Response.succeed();
    }


    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 保养执行分页
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @PostMapping("/pageList")
    @ApiOperation("保养执行分页")
    public Response<?> pageList(@Valid @RequestBody CusUpkeepParam param) {
        return Response.succeed(upkeepService.findPageListByParam(param));
    }


    /**
     * @return com.xinsoft.common.Response<?>
     * @Author Li9527
     * @Description 验收评价
     * @Date 2023/5/18 10:51
     * @params param
     **/
    @PostMapping("/check")
    @ApiOperation("验收评价")
    public Response<?> check(@RequestBody CusUpkeepAudit upkeepAudit) {
        // 只有已完成才能验收
        CusUpkeep cusUpkeep = upkeepService.getById(upkeepAudit.getUpkeepId());
        if (!cusUpkeep.getUpkeepStatus().equals(Const.UPKEEP_STATUS_OVER)) {
            return Response.fail("只有已完成的保养执行才能填写验收评价");
        }
        CusUpkeepAudit cusUpkeepAudit = auditService.getOne(new QueryWrapper<CusUpkeepAudit>().eq("upkeep_id",upkeepAudit.getUpkeepId()));
        if(cusUpkeepAudit != null){
            return Response.fail("该保养执行已经验收过，请不要重复验收");
        }
        // 参数校验
        ValidatorUtils.validateEntity(upkeepAudit);
        upkeepAudit.setApproveTime(new Date());
        upkeepAudit.setApprovePersonId(TokenUtil.getUserId());
        return Response.succeed(auditService.save(upkeepAudit));
    }


}
